package com.boot.security.server.controller.system;

import java.util.List;

import com.boot.security.server.model.mffl.Vo.MfflInformationReplyVo;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflInformationReplyDao;
import com.boot.security.server.model.mffl.MfflInformationReply;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@RequestMapping("/mfflInformationReplys")
public class MfflInformationReplyController {

    @Autowired
    private MfflInformationReplyDao mfflInformationReplyDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public MfflInformationReply save(@RequestBody MfflInformationReply mfflInformationReply) {
        mfflInformationReplyDao.save(mfflInformationReply);

        return mfflInformationReply;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public MfflInformationReply get(@PathVariable Long id) {
        return mfflInformationReplyDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public MfflInformationReply update(@RequestBody MfflInformationReply mfflInformationReply) {
        mfflInformationReplyDao.update(mfflInformationReply);

        return mfflInformationReply;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflInformationReplyDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflInformationReplyVo> list(PageTableRequest request) {
                return mfflInformationReplyDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public ReturnBean delete(@PathVariable Long id) {

        int status = this.mfflInformationReplyDao.delMyReply(id);
        if (status == 0) {
            return new ReturnBean("201", "删除失败");
        }
        this.mfflInformationReplyDao.delMyReplyOfReply(id);
        return new ReturnBean();

    }
}
